1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.io;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.collect.ImmutableSet;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Random;
27
28
29
30
31
32
33 public final class TestByteSource extends ByteSource implements TestStreamSupplier {
34
35 private final byte[] bytes;
36 private final ImmutableSet<TestOption> options;
37
38 private boolean inputStreamOpened;
39 private boolean inputStreamClosed;
40
41 TestByteSource(byte[] bytes, TestOption... options) {
42 this.bytes = checkNotNull(bytes);
43 this.options = ImmutableSet.copyOf(options);
44 }
45
46 @Override
47 public boolean wasStreamOpened() {
48 return inputStreamOpened;
49 }
50
51 @Override
52 public boolean wasStreamClosed() {
53 return inputStreamClosed;
54 }
55
56 @Override
57 public InputStream openStream() throws IOException {
58 inputStreamOpened = true;
59 return new RandomAmountInputStream(new In(), new Random());
60 }
61
62 private final class In extends TestInputStream {
63
64 public In() throws IOException {
65 super(new ByteArrayInputStream(bytes), options);
66 }
67
68 @Override
69 public void close() throws IOException {
70 inputStreamClosed = true;
71 super.close();
72 }
73 }
74 }